﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This uses a LonelyPixel Filter and afterwards
    /// a PerPixel Filter to extract contours
    /// </summary>
    public class ContourExtractionTwoSteps : MacroFilter, IBaseForeBackgroundColorFilter
    {
        protected static Color m_ForegroundColor = Definitions.White;
        protected static Color m_BackgroundColor = Definitions.Black;
        protected static int m_nThreshold = 100;
        protected static int m_nPixelValue = 8;

        protected LonelyPixel m_LonelyPixel = new LonelyPixel();

        /// <summary>
        /// Default Per Pixel Filter
        /// </summary>
        private static BaseImageFilter perPixelFilter =
            new PerPixelFilter(
                delegate(Color _color)
                {
                    return (_color.R > m_nThreshold
                        || _color.G > m_nThreshold
                        || _color.B > m_nThreshold) ? m_ForegroundColor : m_BackgroundColor;
                }
            );

        /// <summary>
        /// Initializes a new instance of the <see cref="ContourExtractionTwoSteps"/> class.
        /// </summary>
        public ContourExtractionTwoSteps()
        {
        }

        /// <summary>
        /// Gets or sets the threshold.
        /// </summary>
        /// <value>The threshold.</value>
        public int Threshold
        {
            get { return m_nThreshold; }
            set { m_nThreshold = value; }
        }

        /// <summary>
        /// Gets or sets the pixel value.
        /// </summary>
        /// <value>The pixel value.</value>
        public int PixelValue
        {
            get { return m_nPixelValue; }
            set { m_nPixelValue = value; }
        }

        /// <summary>
        /// Now this can also be used to specify the iterations for the execution of each filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            perPixelFilter.Execute(_bitmap);
            m_LonelyPixel.Execute(_bitmap);
            return _bitmap;
        }

        #region IBaseForeBackgroundColorFilter Members

        /// <summary>
        /// Gets or sets the color of the background.
        /// </summary>
        /// <value>The color of the background.</value>
        public Color BackgroundColor
        {
            get { return m_BackgroundColor; }
            set { m_BackgroundColor = value; }
        }

        /// <summary>
        /// Gets or sets the foreground.
        /// </summary>
        /// <value>The foreground.</value>
        public Color Foreground
        {
            get{ return m_ForegroundColor; }
            set { m_ForegroundColor = value; ; }
        }

        #endregion
    }
}
